<?php

// 积分商城

namespace app\console\controller;

use think\Url;
use think\Loader;
use think\Request;
use think\Controller;
use app\console\model\IntegralGoods as IntegralGoodsModel;

class IntegralGoods extends Controller
{

    /**
     * 积分商城列表
     * @param Request $request
     * @param IntegralGoodsModel $integralGoods
     * @return mixed
     */
    public function index(Request $request, IntegralGoodsModel $integralGoods)
    {
        // 获取参数
        $inquire = $request->param();

        // 筛选
        $condition['id'] = ['neq', '0'];
        if (!empty($request->param('title'))) $condition['title'] = ['like', '%' . $inquire['title'] . '%'];
        if (!empty($request->param('type'))) $condition['type'] = $inquire['type'];

        $data = $integralGoods->where($condition)->order(['sort' => 'asc','id' => 'desc'])->paginate(10, false, ['query' => $inquire]);


        return $this->fetch('', [
            'mate_title' => '积分商城列表',
            'data'       => $data
        ]);

    }


    /**
     * 新增积分礼品
     * @param Request $request
     * @param IntegralGoodsModel $integralGoods
     * @return mixed
     */
    public function create(Request $request, IntegralGoodsModel $integralGoods)
    {
        if ($request->isPost()) {
            // 获取参数
            $data = $request->param();

            // 验证
            $validate = Loader::validate('IntegralGoods');
            if (!$validate->check($data)) fail($validate->getError());

            $operation = $integralGoods->allowField(true)->save($data);

            if ($operation) {
                win('操作成功', Url::build('IntegralGoods/index'));
            }

            fail('操作失败');

        }


        return $this->fetch('', [
            'mate_title' => '新增积分礼品',

        ]);
    }


    /**
     * 编辑积分商品
     * @param $id
     * @param Request $request
     * @param IntegralGoodsModel $integralGoods
     * @return mixed
     */
    public function update($id, Request $request, IntegralGoodsModel $integralGoods)
    {
        if ($request->isPost()) {

            // 获取参数
            $data = $request->param();

            // 验证
            $validate = Loader::validate('IntegralGoods');
            if (!$validate->check($data)) fail($validate->getError());

            $operation = $integralGoods->allowField(true)->save($data, ['id' => $id]);

            if ($operation) {
                win('操作成功', Url::build('IntegralGoods/index'));
            }

            fail('操作失败');

        }

        $item = $integralGoods::get($id);


        return $this->fetch('create', [
            'mate_title' => '新增积分礼品',
            'item'       => $item

        ]);
    }


    /**
     * 删除积分商品
     * @param $id
     * @param IntegralGoodsModel $integralGoods
     */
    public function destroy($id,IntegralGoodsModel $integralGoods)
    {
        if ($integralGoods::destroy($id)) {
            win('操作成功', Url::build('IntegralGoods/index'));
        }

        fail('操作失败');
    }


    /*
    * 排列顺序
    */
    public function sorting()
    {

        $id = Request::instance()->param('orderId/a');
        $sort = Request::instance()->param('sort/a');

        foreach ($id as $key => $value) {
            IntegralGoodsModel::where('id', $value)->update(['sort' => $sort[$key]]);
        }

        win('操作成功', Url::build('IntegralGoods/index'));
    }

}